/*  
 *  Copyright (c) 2008 Adrian Necimal <necimal@gmail.com>
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://app/jsmodules/sbProperties.jsm");

function LibraryImportExport() {
  this.wrappedJSObject = this;
};

LibraryImportExport.prototype = {
  classDescription: "Necimal's Library Import/Export Service",
  classID: Components.ID("{1D560BD5-9B29-4272-BC59-E5CDDF8963E0}"),
  contractID: "@necimal.com/library/importexportservice;1",
  QueryInterface: XPCOMUtils.generateQI([])
}

LibraryImportExport.prototype.importLibrary = function(library, file) {
    
}

LibraryImportExport.prototype.exportLibrary = function(library, file) {
    // create a new export listener, and enumerate the library
    var listener = new this.ExportEnumerationListener(file);
    library.enumerateAllItems(listener);
}

LibraryImportExport.prototype.ExportEnumerationListener = function(file) {
    this._file = file;
}

LibraryImportExport.prototype.ExportEnumerationListener.prototype = {
    onEnumerationBegin: function(mediaList) {
        // create a new output stream
        if (this._file.exists()) { this._file.remove(false); }
        this._outstream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
        this._outstream.init(this._file, -1, -1, 0);
        
        // start the library xml
        this._nextline = '<?xml version="1.0"?>\n<library id="' + mediaList.guid + '">\n';
        this._outstream.write(this._nextline, this._nextline.length);
        this._propertyenumerator = null;
        this._propertyid = "";
        this._progress = 0;
    },
    
    onEnumeratedItem: function(mediaList, mediaItem) {
        
        this._nextline = '\t<mediaitem id="' + mediaItem.guid + '">\n';
        this._outstream.write(this._nextline, this._nextline.length);
        
        this._propertyenumerator = mediaItem.propertyIDs;
        while (this._propertyenumerator.hasMore()) {
            this._propertyid = this._propertyenumerator.getNext();
            this._nextline = '\t\t<property id="' + this._propertyid + '">' +
                              mediaItem.getProperty(this._propertyid) + '</property>\n';
            this._outstream.write(this._nextline, this._nextline.length);
        }
        
        this._nextline = "\t</mediaitem>\n";
        this._outstream.write(this._nextline, this._nextline.length);
        
        // write to the output file every now and then
        this._progress++;
        if (this._progress % 100 == 0) { this._outstream.flush(); }
    },
    
    onEnumerationEnd: function(mediaList, statusCode) {
        // finish up and close the xml file
        this._nextline = "</library>";
        this._outstream.write(this._nextline, this._nextline.length);
        this._outstream.close();
    }
}

function NSGetModule(compMgr, fileSpec) {
  return XPCOMUtils.generateModule([LibraryImportExport]);
}
